<?php
/**
 * Created by PhpStorm.
 * User: xionghao
 * Date: 2018/8/2
 * Time: 下午5:27
 */

namespace app\portal\controller;


use app\portal\model\DownloadModel;
use app\portal\model\DownloadUserModel;
use cmf\controller\HomeBaseController;

class DownloadController extends HomeBaseController
{
    public function index()
    {
        $downloadModel = new  DownloadModel();
        $downloads = $downloadModel->order('create_time')->paginate(15);
        $this->assign('downloads', $downloads->items());
        $this->assign('page', $downloads->render());
        return $this->fetch(":download");
    }

    public function detail($id)
    {
        $downloadModel = new  DownloadModel();
        $article = $downloadModel->find($id);

        $this->assign('article', $article);

        return $this->fetch(':download_detail');

    }

    public function download($id)
    {
        $downloadModel = new  DownloadModel();
        $article = $downloadModel->find($id);
        $download_user = session('download_user');

        if ($article['is_vip'] && empty($download_user)) {
            $this->error('需要登录');
        } else {
            $this->success('获取成功', cmf_get_asset_url($article['file']));
        }

    }

    public function login()
    {
        $username = $this->request->request('username');
        $password = $this->request->request('password');
        if (!$username) {
            $this->error('账号不能为空');
        }
        if (!$password) {
            $this->error('密码不能为空');
        }
        $user = DownloadUserModel::where("username", $username)->where('password', $password)->find();
        if ($user) {
            if ($user['status'] == 0){
                $this->error('账号已禁用,请联系管理员');
            }else {
                session('download_user',$user);
                $this->success('登录成功');
            }
        }else {
            $this->error('账号不存在');
        }

    }
}